/*
* @file recoder.h
* @brief Header file for the Recorder class
* @details This class handles audio recording using I2S and SPIFFS.
*          It provides methods to initialize the recorder, record audio, read PCM files,
*          and clear the recorder file.
* @author feiniao
* @date 2025-5-07
* @version 1.0
*/


#ifndef RECORDER_H
#define RECORDER_H

#include "bsp_i2s.h"
#include "esp_log.h"
#include "esp_spiffs.h"
#include <string.h>
#include <stdio.h>

#define TAG "Recorder"

#define PCM_FILE_NAME  "/spiffs/audio.pcm"      // PCM file name

// i2s pin configuration
#define INMP441_BCLK_PIN       5
#define INMP441_WS_PIN         4
#define INMP441_SD_PIN         6
#define SAMPLE_RATE            16000

class Recorder {
private:
    BSP_I2S recorder;
    void init_spiffs();
    void format_spiffs();
public:
    Recorder();

    ~Recorder();

    size_t int32t_record(float seconds);
    size_t int16t_record(float seconds);

    void read_pcm_file_int32t(int32_t* buffer);
    void read_pcm_file_int16t(int16_t* buffer);

    void clear_recorder_File();

    int16_t read_single_Sample_int16t();
    int32_t read_single_Sample_int32t();
};

#endif


/*
example usage:
// recorder test
#include "recoder.h"
extern "C" void app_main() {
    Recorder recorder;
    size_t size = recorder.int16t_record(5.0f); // 录制5秒音频
    ESP_LOGI(TAG, "录制的音频样本数: %zu 个", size);
    int16_t* buffer = (int16_t *)heap_caps_malloc(size * sizeof(int16_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
    if (buffer) {
        recorder.read_pcm_file_int16t(buffer);
        for (int i = 0; i < 20; i++) {
            ESP_LOGI(TAG, "读取的音频数据: %d", buffer[i]);
        }
        heap_caps_free(buffer);
    } else {
        ESP_LOGE(TAG, "内存分配失败");
    }
    recorder.clear_recorder_File(); // 清空录音文件
    ESP_LOGI(TAG, "录音文件已清空");
}
*/

